<%@page import="java.io.File"%>
<%@ page language="java" import="java.util.*,news.dao.*,news.pojo.*,news.util.*" pageEncoding="UTF-8"%>
<%@page import="org.apache.commons.fileupload.FileItem"%>
<%@page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%@page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@page import="org.apache.commons.fileupload.FileItemFactory"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'domodifynews.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

</head>

<body>
	<%
    request.setCharacterEncoding("utf-8");
    Integer nid=Integer.parseInt(request.getParameter("nid"));        
    DiskFileItemFactory df=new DiskFileItemFactory();
    ServletFileUpload upload=new ServletFileUpload(df);
    upload.setHeaderEncoding("utf-8");
    List<FileItem> list=upload.parseRequest(request);
    News news=new News();
    for(FileItem fi:list){
      if(!fi.isFormField()){//是文件域
     // out.print("<br/>");
      String picname=fi.getName();
      if(!picname.equals("")){
      String houzhui=picname.substring(picname.lastIndexOf('.'));
      String newfilename=UUID.randomUUID()+houzhui;
       String filepath=application.getRealPath("upload");
       fi.write(new File(filepath,newfilename));
      // out.print("图片保存的地址应该是upload/"+newfilename);
       //out.print("<img src='images/"+newfilename+"'/>");
       news.setNpicpath("upload/"+newfilename);   
       }else{
       news.setNpicpath("");
       }
      }else{
      if(fi.getFieldName().equals("nid"))
      news.setNid(Integer.parseInt( fi.getString("utf-8")));
      if(fi.getFieldName().equals("ntitle"))
      news.setNtitle(fi.getString("utf-8"));
      if(fi.getFieldName().equals("nauthor"))
      news.setNauthor(fi.getString("utf-8"));
      if(fi.getFieldName().equals("nsummary"))
      news.setNsummary(fi.getString("utf-8"));
      if(fi.getFieldName().equals("ncontent"))
      news.setNcontent(fi.getString("utf-8"));
       if(fi.getFieldName().equals("ntid"))
      news.setNtid(Integer.parseInt( fi.getString("utf-8")));
      }
       //out.print("</p>");
    } %>
<%
int re=NewsDao.nd.update(news);
if(re==1){
out.print("<script>location='modifynews.jsp?nid="+nid+"';alert('修改成功！');</script>");   
}else{
out.print("<script>location='modifynews.jsp?nid="+nid+"';alert('修改失败！');</script>"); 

}


 %>

</body>
</html>
